precision mediump float;

const int GAUSSIAN_SAMPLES = 101;
varying vec2 vTextureCoordinate;
uniform sampler2D inputImageTexture;
uniform float wOffset;
uniform float hOffset;
uniform int horizontal;
uniform int kernelSize; // 核的大小
uniform int box; // 盒状模糊
uniform float kernelWeights[GAUSSIAN_SAMPLES]; // 假设最大核大小为 1001

void main() {
     // 获取原始颜色和alpha值
    vec4 originalColor = texture2D(inputImageTexture, vTextureCoordinate);
    float originalAlpha = originalColor.a;
    vec4 color = vec4(0.0, 0.0, 0.0, 0.0);
    if (box == 0) {
        vec2 texOffset = horizontal == 1 ? vec2(wOffset, 0.0) : vec2(0.0, hOffset);
        color = texture2D(inputImageTexture, vTextureCoordinate) * kernelWeights[0];
        for(int i = 1; i < GAUSSIAN_SAMPLES; i++){
            if (i < kernelSize) {
                color += texture2D(inputImageTexture, vTextureCoordinate + texOffset * float(i)) * kernelWeights[i];
                color += texture2D(inputImageTexture, vTextureCoordinate - texOffset * float(i)) * kernelWeights[i];
            }
        }
        color.a = 5.0;
    } else {
        // 盒状模糊
        vec2 tex_offset = vec2(wOffset, wOffset); // 偏移量，用于访问周围的像素
        vec3 result = originalColor.rgb; // 中心像素的颜色

        // 访问周围的8个像素并累加它们的颜色值
        result += texture2D(inputImageTexture, vTextureCoordinate + vec2(-tex_offset.x, -tex_offset.y)).rgb;
        result += texture2D(inputImageTexture, vTextureCoordinate + vec2( 0.0, -tex_offset.y)).rgb;
        result += texture2D(inputImageTexture, vTextureCoordinate + vec2( tex_offset.x, -tex_offset.y)).rgb;

        result += texture2D(inputImageTexture, vTextureCoordinate + vec2(-tex_offset.x,  0.0)).rgb;
        result += texture2D(inputImageTexture, vTextureCoordinate + vec2( tex_offset.x,  0.0)).rgb;

        result += texture2D(inputImageTexture, vTextureCoordinate + vec2(-tex_offset.x,  tex_offset.y)).rgb;
        result += texture2D(inputImageTexture, vTextureCoordinate + vec2( 0.0,  tex_offset.y)).rgb;
        result += texture2D(inputImageTexture, vTextureCoordinate + vec2( tex_offset.x,  tex_offset.y)).rgb;

        result /= 9.0; // 计算平均值

        color = vec4(result, 1.0);
    }
    
    gl_FragColor = color;
}